package com.easyadmin.service.impl;

import com.easyadmin.entity.enums.PageSize;
import com.easyadmin.entity.po.SysTaskRunLog;
import com.easyadmin.entity.query.SysTaskRunLogQuery;
import com.easyadmin.entity.vo.PaginationResult;
import com.easyadmin.entity.vo.SimplePage;
import com.easyadmin.exception.BusinessException;
import com.easyadmin.mappers.SysTaskRunLogMapper;
import com.easyadmin.service.SysTaskRunLogService;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * 业务接口实现
 */
@Service("sysTaskRunLogService")
public class SysTaskRunLogServiceImpl implements SysTaskRunLogService {

    @Resource
    private SysTaskRunLogMapper<SysTaskRunLog, SysTaskRunLogQuery> sysTaskRunLogMapper;

    /**
     * 根据条件查询列表
     */
    @Override
    public List<SysTaskRunLog> findListByParam(SysTaskRunLogQuery param) {
        return this.sysTaskRunLogMapper.selectList(param);
    }

    /**
     * 根据条件查询列表
     */
    @Override
    public Integer findCountByParam(SysTaskRunLogQuery param) {
        return this.sysTaskRunLogMapper.selectCount(param);
    }

    /**
     * 分页查询方法
     */
    @Override
    public PaginationResult<SysTaskRunLog> findListByPage(SysTaskRunLogQuery param) {
        int count = this.sysTaskRunLogMapper.selectCount(param);
        int pageSize = param.getPageSize() == null ? PageSize.SIZE15.getSize() : param.getPageSize();
        int pageNo = 0;
        if (null != param.getPageNo()) {
            pageNo = param.getPageNo();
        }
        SimplePage page = new SimplePage(pageNo, count, pageSize);
        param.setSimplePage(page);
        List<SysTaskRunLog> list = this.sysTaskRunLogMapper.selectList(param);
        PaginationResult<SysTaskRunLog> result = new PaginationResult<>(count, pageSize, pageNo, page.getPageTotal(), list);
        return result;
    }

    /**
     * 新增
     */
    @Override
    public Integer add(SysTaskRunLog bean) throws BusinessException {
        try {
            return this.sysTaskRunLogMapper.insert(bean);
        } catch (DuplicateKeyException e) {
            throw new BusinessException("信息已经存在");
        }
    }

    /**
     * 批量新增
     */
    @Override
    public Integer addBatch(List<SysTaskRunLog> listBean) {
        if (listBean == null || listBean.isEmpty()) {
            return 0;
        }
        return this.sysTaskRunLogMapper.insertBatch(listBean);
    }

    /**
     * 批量新增或者修改
     */
    @Override
    public Integer addOrUpdateBatch(List<SysTaskRunLog> listBean) {
        if (listBean == null || listBean.isEmpty()) {
            return 0;
        }
        return this.sysTaskRunLogMapper.insertOrUpdateBatch(listBean);
    }

    /**
     * 根据Id修改
     */
    @Override
    public Integer updateById(SysTaskRunLog bean, Integer id) {
        return this.sysTaskRunLogMapper.updateById(bean, id);
    }


    /**
     * 根据Id删除
     */
    @Override
    public Integer deleteById(Integer id) {
        return this.sysTaskRunLogMapper.deleteById(id);
    }


    /**
     * 根据Id获取对象
     */
    @Override
    public SysTaskRunLog getSysTaskRunLogById(Integer id) {
        return this.sysTaskRunLogMapper.selectById(id);
    }
}